package org.lt.ttkv;

import android.annotation.SuppressLint;
import android.util.Base64;
import android.util.Log;

import androidx.annotation.NonNull;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.charset.StandardCharsets;

/**
 * @ClassName: KV
 * @Description: 类作用描述
 * @Author: wtl
 * @CreateDate: 2023/7/5 19:35
 * @UpdateUser: 更新者 wtl
 * @UpdateDate: 2023/7/5 19:35
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class KV<T> {
    public final String groupName;
    public final String keyName;
    public T value;
    public String clazz=String.class.getCanonicalName();
    public KV(String groupName, String keyName,String clazz) {
        this.groupName = groupName;
        this.keyName = keyName;
        this.clazz = clazz;
    }
    public T get() {
        if (value == null) {
            if (clazz.equals(Integer.class.getCanonicalName())) {
                value = (T) KVConfigs.getKeyValueTool(groupName).getInteger(keyName);
            } else if (clazz.equals(String.class.getCanonicalName())) {
                value = (T) KVConfigs.getKeyValueTool(groupName).getString(keyName);
            } else if (clazz.equals(Float.class.getCanonicalName())) {

                value = (T) KVConfigs.getKeyValueTool(groupName).getFloat(keyName);
            } else if (clazz.equals(Long.class.getCanonicalName())) {

                value = (T) KVConfigs.getKeyValueTool(groupName).getLong(keyName);
            } else if (clazz.equals(Boolean.class.getCanonicalName())) {

                value = (T) KVConfigs.getKeyValueTool(groupName).getBoolean(keyName);
            } else  {//if (clazz.equals(BaseSerializable.class.getCanonicalName()))
                String s = KVConfigs.getKeyValueTool(groupName).getString(keyName);
                value = (T) base64StringToObject(s);
            }
        }
        return value;
    }

    public void set(@NonNull T value) {
        this.value = value;
        if (clazz.equals(Integer.class.getCanonicalName())) {
            KVConfigs.getKeyValueTool(groupName).put(keyName, (Integer) value);
        } else if (clazz.equals(String.class.getCanonicalName())) {
            KVConfigs.getKeyValueTool(groupName).put(keyName, (String) value);
        } else if (clazz.equals(Float.class.getCanonicalName())) {
            KVConfigs.getKeyValueTool(groupName).put(keyName, (Float) value);
        } else if (clazz.equals(Long.class.getCanonicalName())) {
            KVConfigs.getKeyValueTool(groupName).put(keyName, (Long) value);
        } else if (clazz.equals(Boolean.class.getCanonicalName())) {
            KVConfigs.getKeyValueTool(groupName).put(keyName, (Boolean) value);
        } else {String s = objectToBase64String(value);
            KVConfigs.getKeyValueTool(groupName).put(keyName, s);
        }

    }

    @SuppressLint("LongLogTag")
    private String objectToBase64String(Object o) {
        String s = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(o);
            oos.close();
            bos.close();
            s = Base64.encodeToString(bos.toByteArray(), Base64.DEFAULT);
        } catch (Exception e) {
            Log.e("--- keyValue - objectToBase64String -- ", e.toString());
        }
        return s;
    }

    @SuppressLint("LongLogTag")
    private Object base64StringToObject(String s) {
        Object o = null;
        try {
            byte[] gameByte = Base64.decode(s.getBytes(StandardCharsets.UTF_8), Base64.DEFAULT);
            ByteArrayInputStream bis = new ByteArrayInputStream(gameByte);
            ObjectInputStream ois = new ObjectInputStream(bis);
            o = ois.readObject();
            bis.close();
            ois.close();
        } catch (Exception e) {
            Log.e("--- keyValue - base64StringToObject -- ", e.toString());
        }
        return o;
    }
}
